package com.citycloud.ccuap.tc.sync.aspect;

import com.citycloud.ccuap.commons.db.SqlRestrictions;
import com.citycloud.ccuap.commons.json.JSONData;
import com.citycloud.ccuap.commons.sys.deptment.DeptmentService;
import com.citycloud.ccuap.commons.sys.entity.SysDeptment;
import com.citycloud.ccuap.commons.sys.entity.SysProject;
import com.citycloud.ccuap.commons.sys.project.IProjectService;
import com.citycloud.ccuap.tc.sync.constant.GlobalSyncOperation;
import com.citycloud.ccuap.tc.sync.constant.GlobalSyncType;
import com.citycloud.ccuap.tc.sync.dto.DepartmentMessageDTO;
import com.citycloud.ccuap.tc.sync.dto.DeptIdProjectIdDTO;
import com.citycloud.ccuap.tc.sync.dto.SyncDataDto;
import com.citycloud.ccuap.tc.sync.service.IMessageService;
import org.apache.commons.lang.StringUtils;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Arrays;
import java.util.List;

@Aspect
@Component
public class DeptUpdateAspect {
    @Autowired
    private DeptmentService deptmentService;
    @Autowired
    private IMessageService messageService;
    @Autowired
    private IProjectService projectService;

    @Pointcut("execution(* com.citycloud.ccuap.commons.sys.deptment.DeptmentController.update())")
    public void updateDept() {
    }

    @AfterReturning(value = "updateDept()", returning = "key")
    public void doUpdateDept(JoinPoint joinPoint, Object key) throws Throwable {
        if (key instanceof JSONData) {
            JSONData jsonData = (JSONData) key;
            if (jsonData.getData() instanceof SysDeptment) {
                SysDeptment dept = (SysDeptment) jsonData.getData();
                sendMessage(dept);
            }
        }

    }

    private void sendMessage(SysDeptment department) {
        if (department.getProjectId() == null) {
            //查询关联部门站点
            List<DeptIdProjectIdDTO> deptmentRels = getProjectDepts(Arrays.asList(department.getDeptId()));
            for (DeptIdProjectIdDTO dto : deptmentRels) {
                if (dto.getMqOff()) {
                    DepartmentMessageDTO deptMessage = new DepartmentMessageDTO();
                    deptMessage.build(department);
                    deptMessage.setProjectId(dto.getProjectId());

                    send(dto.getProjectId(), deptMessage);
                }
            }
        } else {
            SysProject project = projectService.findById(department.getProjectId());
            if (project != null && project.getMqOff()) {
                DepartmentMessageDTO deptMessage = new DepartmentMessageDTO();
                deptMessage.build(department);

                send(department.getProjectId(), deptMessage);
            }
        }
    }

    private List<DeptIdProjectIdDTO> getProjectDepts(List<Long> deptIds) {
        String deptIdsStr = StringUtils.join(deptIds, ",");
        String sql = "select rel.rel_id as relId,rel.dept_id as deptId,rel.project_id as projectId, pro.mq_off as mqOff from sys_project_deptment_rel rel ,sys_project pro where rel.project_id = pro.project_id ";
        sql += SqlRestrictions.in("rel.dept_id", deptIdsStr);
        List<DeptIdProjectIdDTO> deptmentRels = (List<DeptIdProjectIdDTO>) deptmentService.selectList(DeptIdProjectIdDTO.class, sql);
        return deptmentRels;
    }

    private void send(Long projectId, DepartmentMessageDTO dto) {
        SyncDataDto<DepartmentMessageDTO> result = SyncDataDto.of(GlobalSyncType.OBJECT_TYPE_DEPT,
                GlobalSyncOperation.UPDATED, projectId, dto);
        messageService.sendMessageByTopic(result,GlobalSyncType.TOPIC_USER_DEPT);
    }
}
